package com.warmheart.core.util;

import java.math.BigDecimal;

/***
 * 由于Java的简单类型不能够精确的对浮点数进行运算，这个工具类提供精 确的浮点数运算，包括加减乘除和四舍五入。
 * 
 */
public class ArithBigDecimal {

    public static BigDecimal add(double v1, double v2, int scale) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal add(BigDecimal v1, BigDecimal v2, int scale) {
        return v1.add(v2).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal sub(double v1, double v2, int scale) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal sub(BigDecimal v1, BigDecimal v2, int scale) {
        return v1.subtract(v2).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal mul(double v1, double v2, int scale) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal mul(BigDecimal v1, BigDecimal v2, int scale) {
        return v1.multiply(v2).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal div(double v1, double v2, int scale) {
        return divHalfUp(v1, v2, scale);
    }

    public static BigDecimal divHalfUp(double v1, double v2, int scale) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 提供精确的小数位四舍五入处理。
     * 
     * @param v
     *            需要四舍五入的数字
     * @param scale
     *            小数点后保留几位
     * @return 四舍五入后的结果
     * @throws Exception
     */
    public static double round(BigDecimal v, int scale) throws Exception {
        if (scale < 0) {
            throw new Exception("精确位数必须大于0");
        }
        BigDecimal one = new BigDecimal("1");
        return v.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 比较两个double类型数字是否相等，相等返回true
     * 
     * @param v1
     *            参数1
     * @param v2
     *            参数2
     * @return
     */
    public static boolean compareDouble(double v1, double v2) {
        BigDecimal d1 = new BigDecimal(Double.toString(v1));
        BigDecimal d2 = new BigDecimal(Double.toString(v2));
        if (d1.compareTo(d2) == 0) {// 两double 数字相同
            return true;
        }
        return false;
    }

    /**
     * 比较两个BigDecimal类型数字是否相等，相等返回true
     * 
     * @param v1
     *            参数1
     * @param v2
     *            参数2
     * @return
     */
    public static boolean compareBigDecimal(BigDecimal v1, BigDecimal v2) {
        if (v1.compareTo(v2) == 0) {// 两BigDecimal 数字相同
            return true;
        }
        return false;
    }
};
